package com.wx.mall.goods.controller;


import com.wx.mall.common.api.CommonPage;
import com.wx.mall.common.api.CommonResult;
import com.wx.mall.common.api.PageVo;
import com.wx.mall.common.exceptions.ApiException;
import com.wx.mall.common.vo.goods.SpuVo;
import com.wx.mall.goods.entity.Spu;
import com.wx.mall.goods.service.ISpuService;
import com.wx.mall.goods.vo.req.SpuMatchVo;
import com.wx.mall.goods.vo.resp.DicountVo;
import com.wx.mall.goods.vo.resp.SpuCountVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ljl
 * @since 2020-04-17
 */
@RestController
@RequestMapping("/spu")
@Api(tags = "商品SPU相关接口")
public class SpuController {
    @Autowired
    private ISpuService service;


    @GetMapping("/getSpuByCategoryId")
    @ApiOperation("根据分类ID获取数据")
    public CommonResult<DicountVo> getSpuByCategoryId(String categoryId){
        System.out.println(categoryId);
        DicountVo vo=service.getSpuByCategoryId(categoryId);
        return CommonResult.success(vo);

    }


    @GetMapping("/getByName")
    @ApiOperation("根据名称获取到数据(模糊查询)")
    public CommonResult<List<Spu>> getByName(String name){

        List<Spu> list=service.getByName(name);
        return CommonResult.success(list);

    }

    @GetMapping("/getByNameT")
    @ApiOperation("根据名称获取到数据(精准查询)")
    public CommonResult<Spu> getByNameT(String name){

        Spu spu=service.getByNameT(name);
        return CommonResult.success(spu);

    }

    @GetMapping("/pageList")
    @ApiOperation("分页获取商品SPU数据")
    public CommonResult pageList(@ApiParam("分页vo") @Valid PageVo vo) {
        CommonPage<SpuVo> list = service.pageList(vo);
        return CommonResult.success(list);
    }


    @GetMapping("/count")
    @ApiOperation("分页获取商品SPU数据")
    public CommonResult count() {
        SpuCountVo countVo = service.countSpu();
        return CommonResult.success(countVo);
    }

//    @GetMapping("/pageList/{id}")
//    @ApiOperation("通过SPU ID 分页获取商品SKU数据")
//    public CommonResult pageListById(@ApiParam("分页vo") @Valid PageVo vo, @ApiParam("商品SPU id") @PathVariable("id") @NotNull(message = "id不能为空") Integer id) {
//        CommonPage<SkuVo> list = service.pageListBySpuId(vo,id);
//        return CommonResult.success();
//    }

    @PostMapping("/pageList/match")
    @ApiOperation("通过条件分页获取商品SPU数据")
    public CommonResult pageMatchList(@ApiParam("分页vo") @Valid SpuMatchVo vo) {
        CommonPage<SpuVo> match = service.pageMatch(vo);
        return CommonResult.success(match);
    }


    @PostMapping("/pageList/matchRecycle")
    @ApiOperation("通过条件分页获取商品SPU数据(商品回收站)")
    public CommonResult pageMatchRecycle(@ApiParam("分页vo") @Valid SpuMatchVo vo) {
        CommonPage<SpuVo> match = service.pageMatchRecycle(vo);
        return CommonResult.success(match);
    }


    @GetMapping("/getSpu/{id}")
    @ApiOperation("根据获取商品SPU数据")
    public CommonResult getEntity(@ApiParam("SPU id") @PathVariable("id") @NotNull(message = "id不能为空") Integer id) {
        SpuVo vo = service.selectEntity(id);
        return CommonResult.success(vo);
    }


    @DeleteMapping("/deleteSpu/{id}")
    @ApiOperation("根据id删除商品SPU数据")
    public CommonResult deleteEntity(@ApiParam("SPU id") @PathVariable("id") @NotNull(message = "id不能为空") Integer id) {
        service.deleteEntity(id);
        return CommonResult.success();
    }

    @DeleteMapping("/deleteSpuTwo/{id}")
    @ApiOperation("根据id删除商品SPU数据(物理删除)")
    public CommonResult deleteEn(@ApiParam("SPU id") @PathVariable("id") @NotNull(message = "id不能为空") Integer id) {
        service.removeById(id);
        return CommonResult.success();
    }



    @PutMapping("/updateSpu")
    @ApiOperation("根据id更新商品SPU数据")
    public CommonResult update(@Valid SpuVo vo) {
        service.updateEntity(vo);
        return CommonResult.success();
    }

    @PostMapping("/addSpu")
    @ApiOperation("新增商品SPU信息 并返回新增的id")
    public CommonResult add(@Valid SpuVo vo) {
        Integer id = service.insertEntity(vo);
        return CommonResult.success(id);
    }

    // ElementUI el-upload 带参数上传文件 后台接受方式
    @PostMapping("/uploadImage")
    @ApiOperation("商品SPU图片上传")
    public CommonResult uploadImage(@ApiParam("image文件") @RequestParam("files") MultipartFile[] files, @ApiParam("其他formData参数") @RequestParam Map<String, String> param) {
        //获取http服务访问端口
//        String logoUrlPrefix = req.getRequestURL().toString().replace(req.getRequestURI(), "");

        String uuid = "";
        if (param.get("spuId") != null) {
            uuid = param.get("spuId").trim();
        }
        List<Map<String, String>> list = new ArrayList<>();

        // 文件列表是否为空
        if (files.length > 0) {
            for (MultipartFile file : files) {
                String name = file.getOriginalFilename();
                if (uuid != null && !uuid.equals("")) {
                    name = "spuId=" + uuid + "&fileName=" + name;
                }
                list.add(service.uploadImage(file, name));
            }
        } else {
            throw new ApiException("上传的文件列表为空！");
        }
        return CommonResult.success(list);
    }

    // ElementUI el-upload 带参数上传文件 后台接受方式
    @PostMapping("/uploadImage/editor")
    @ApiOperation("商品详情图片上传")
    public CommonResult uploadEditor(@ApiParam("image文件") @RequestParam("file") MultipartFile file) {
        String name = file.getOriginalFilename();
        Map<String, String> map = service.uploadImage(file, name);
        return CommonResult.success(map);
    }

    @DeleteMapping("/deleteImage")
    @ApiOperation("商品SPU图片上传")
    public CommonResult deleteImage(@ApiParam("image名称") @RequestParam("imgName") String imgName, @ApiParam("spu编号") @RequestParam("spuId") String spuId) {
        //获取http服务访问端口
//        String logoUrlPrefix = req.getRequestURL().toString().replace(req.getRequestURI(), "");
        String fileName = "spuId=" + spuId + "&fileName=" + imgName;
        return CommonResult.success(service.deleteImage(fileName));
    }

}

